<?php

namespace Xin\Menu\Laravel;

use Illuminate\Support\ServiceProvider;
use Xin\Menu\Contracts\Factory as MenuFactory;
use Xin\Menu\MenuManager;

class MenuServiceProvider extends ServiceProvider
{
	/**
	 * Register any application services.
	 *
	 * @return void
	 */
	public function register()
	{
		$this->registerManager();

		$this->registerCommands();
	}

	/**
	 * 注册站点配置管理器
	 * @return void
	 */
	protected function registerManager()
	{
		$this->app->singleton(MenuManager::class, function () {
			$manager = new MenuManager(
				$this->app['config']->get('menu'),
			);

			$this->registerDrivers($manager);

			$manager->setContainer($this->app);

			return $manager;
		});
		$this->app->alias(MenuManager::class, 'menu');
		$this->app->alias(MenuManager::class, MenuFactory::class);

		$this->app->singleton('menu.driver', function ($app) {
			return $app['menu']->menu();
		});
	}

	/**
	 * 注册菜单驱动
	 */
	protected function registerDrivers(MenuManager $manager)
	{
		$manager->extend('model', function ($name, $config) {
			return new ModelRepository($config);
		});
	}

	protected function registerCommands()
	{
		$this->commands([
		]);
	}
}
